# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: http_utils.py
Author: kij
Create Date: 2021/3/20
-------------------------------------------------
"""

import hashlib
import os
import struct
import time


def send_data(conn, msg):
	"""
	发送数据
	:return:
	"""
	# 数据转成二进制
	msg2bytes = msg.encode('utf-8')
	msg_size = len(msg2bytes)
	# 拿到字节长度的四个字节 先发送
	conn.sendall(struct.pack('i', msg_size))
	# 再发送具体数据
	conn.sendall(msg2bytes)
	print('\033[0;34m发送:%s\033[0m' % msg)


def get_head_len(conn):
	"""
	读取四个字节 拿到接下来将要读取的长度
	:param conn: socket链接类
	:return:接下来将要读取的数据长度 整型数据
	"""
	bytes_list = []
	has_read_size = 0
	while has_read_size < 4:
		check = conn.recv(4 - has_read_size)
		# 如果没有数据说明断开链接
		if not check:
			return False
		has_read_size += len(check)
		bytes_list.append(check)
	header = b''.join(bytes_list)
	next_size = struct.unpack('i', header)[0]
	print('\033[0;31m数据大小:%s\033[0m' % next_size)
	return next_size


def recv_data(conn, read_size=1024):
	"""
	读取请求
	:param conn: socket链接类
	:param read_size: 默认读取的长度
	:return: 返回数据 字节数据
	"""
	next_size = get_head_len(conn)
	if not next_size:
		return False
	all_read_size = 0
	bytes_list = []
	while all_read_size < next_size:
		reads_size = read_size if next_size - all_read_size > read_size else next_size - all_read_size
		# 读取字节数据
		bytes_file = conn.recv(reads_size)
		# 添加到字节列表
		bytes_list.append(bytes_file)
		all_read_size += len(bytes_file)
	# 拼接字节数据成一个整体
	res = b''.join(bytes_list)
	print('\033[0;31m收到:%s\033[0m' % res)
	return res


def send_file(conn, file_path, default_read_size=1024):
	"""发送文件类型数据,接受的是一个处理好了的文件实体类"""
	# 先发送四个字段文件的大小
	file_size = os.stat(file_path).st_size
	conn.sendall(struct.pack('i', file_size))
	# 打开文件一边读一边发
	all_cum = 0
	with open(file_path, mode='rb') as file:
		already_size = 0
		while already_size < file_size:
			read_size = default_read_size if file_size - already_size > default_read_size else file_size - already_size
			time.sleep(0.4)
			read_bytes = file.read(read_size)
			# 发送读取的文件字节
			conn.sendall(read_bytes)
			already_size += len(read_bytes)
			# # 计算百分比的值
			bfb = int(round(already_size / file_size, 2) * 100)
			percent = "{}%".format(bfb)
			if bfb > all_cum:
				print("\r" + "#" * int(bfb / 2) + percent, end='', flush=True)
			all_cum = bfb
		print()


def save_file(conn, file_path, default_read_size=1024):
	"""
	保存文件
	:return:md5校验码
	"""

	next_size = get_head_len(conn)
	if not next_size:
		return False
	already_size = 0
	md5 = hashlib.md5()
	all_cum = 0
	with open(file_path, mode='wb') as will_save_file:
		while already_size < next_size:
			read_size = default_read_size if next_size - already_size > default_read_size else next_size - already_size
			# 读取数据
			time.sleep(0.4)
			read_bytes = conn.recv(read_size)
			md5.update(read_bytes)
			will_save_file.write(read_bytes)
			already_size += len(read_bytes)
			# 计算百分比的值
			bfb = int(round(already_size / next_size, 2) * 100)
			percent = "{}%".format(bfb)
			if bfb > all_cum:
				print("\r" + "#" * bfb + percent, end='', flush=True)
			all_cum = bfb
	# 返回一个md5校验码
	return md5.hexdigest()


def re_send_file(conn, file_path, already_size, default_read_size=1024):
	"""发送文件类型数据,接受的是一个处理好了的文件实体类"""
	# 先发送四个字段文件的大小  全部文件大小和已接受的文件大小相减 就是剩余的文件大小
	file_size = os.stat(file_path).st_size
	conn.sendall(struct.pack('i', file_size - already_size))
	# 打开文件一边读一边发
	all_cum = 0
	# 空字节用于装数据

	with open(file_path, mode='rb') as file:
		# 设置光标到已下载的文件位置
		file.seek(already_size)
		while already_size < file_size:
			read_size = default_read_size if file_size - already_size > default_read_size else file_size - already_size

			read_bytes = file.read(read_size)
			# 发送读取的文件字节
			conn.sendall(read_bytes)
			already_size += len(read_bytes)
			# # 计算百分比的值
			bfb = int(round(already_size / file_size, 2) * 100)
			percent = "{}%".format(bfb)
			if bfb > all_cum:
				print("\r" + "#" * bfb + percent, end='', flush=True)
			all_cum = bfb


def re_save_file(conn, file_path,file_size, already_size, default_read_size=1024):
	"""
	保存文件
	:return:md5校验码
	"""
	next_size = get_head_len(conn)
	if not next_size:
		return False
	m = hashlib.md5()
	all_cum = 0
	with open(file_path, mode='ab') as will_save_file:
		while already_size < file_size:
			read_size = default_read_size if next_size - default_read_size > default_read_size else next_size - default_read_size
			# 读取数据
			read_file = conn.recv(read_size)
			m.update(read_file)
			will_save_file.write(read_file)
			already_size += len(read_file)
			# 计算百分比的值
			bfb = int(round(already_size / file_size, 2) * 100)
			percent = "{}%".format(bfb)
			if bfb > all_cum:
				print("\r" + "#" * bfb + percent, end='', flush=True)
			all_cum = bfb